﻿#ifndef UDPCODEC_H
#define UDPCODEC_H

#include <qobject.h>
#include "ObjAutoInject.h"
#include "UdpCodecBase.h"
#include <qnetworkdatagram.h>
#include <qhash.h>

#include "LogSaver.h"

class UdpIOer;
class RegisterCenter_Device;
class UdpController;
class UdpCodec : public QObject,public ObjAutoInject,public UdpCodecBase
{
  Q_OBJECT
/*
数据类型1字节，
消息ID2字节，
设备ID4字节，
时间戳ID2字节
载荷
*/
  friend class UdpController;
private://property
  QHash<unsigned char,UdpCodecBase*> codecMap;//子解码器映射
  const int udpHeadSize=9;//固定头部大小
private://dependency
  UdpIOer* udpIOer=Q_NULLPTR;
  RegisterCenter_Device* registerCenter_Device=Q_NULLPTR;
  UdpController* udpController=Q_NULLPTR;
  LogSaver* logSaver=LogSaver::getLogSaver();
public:
  explicit UdpCodec();
  virtual ~UdpCodec();
signals:
private:
  void code(DataPackage_UdpCodec* dataPackageInfo);
public:
  bool code(uint deviceId, ushort msgId, uchar type, QByteArray data,bool emergency=false);
  virtual DataPackage_UdpCodec* decode(DataPackage_UdpCodec* dataPackageInfo_Device)override;
private slots:
  void slot_decode(QNetworkDatagram qNetworkDatagram);
public://geter and seter
  int getUdpHeadSize();
  UdpIOer* getUdpIOer();
  UdpCodec* setUdpIOer(UdpIOer* udpIOer);
  RegisterCenter_Device* getRegisterCenter_Device()const;
  UdpCodec* setRegisterCenter_Device(RegisterCenter_Device* const registerCenter_Device);
  UdpController* getUdpController()const;
  UdpCodec* setUdpController(UdpController*const udpController);
public:
  virtual void objectAutoInjection(QHash<QString,QObject*>* objMap)override;
};

#endif // UDPCODEC_H
